package com.hub.grpc.client.controller;


import com.hub.grpc.client.service.ChatClientService;
import com.hub.grpc.proto.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ author 飞天蝙蝠
 * @ date & time 2024/12/30 16:18
 */
@Slf4j
@RestController
@RequiredArgsConstructor
public class GrpcInvokeController {

    private final HelloServiceGrpc.HelloServiceBlockingStub helloServiceBlockingStub;

    private final UserServiceGrpc.UserServiceBlockingStub userServiceBlockingStub;

    private final ChatClientService chatClientService;

    @GetMapping("/hello")
    public String invokeHelloService() {
        HelloResponse response = helloServiceBlockingStub.hello(HelloRequest.newBuilder().setName("Shawn").setAge(18).addHobbies("看书").addHobbies("游泳").putTags("长相", "帅气").build());
        String greeting = response.getGreeting();
        log.info("hello result: {}", greeting);
        return greeting;
    }

    @GetMapping("/user")
    public String invokeUserService() {
        UserResponse response = userServiceBlockingStub.query(UserRequest.newBuilder().setName("Shawn").build());
        log.info("user name: {}, age:{}, address:{}", response.getName(), response.getAge(), response.getAddress());
        return response.getName();
    }

    @GetMapping("/chat")
    public String invokeChatService() {
        return chatClientService.chat("你好，我是Shawn", 1, 1000);
    }
}
